﻿using System;
using System.Collections.Generic;
using System.Text;
using MongoDB.Bson;
using MongoDB.Driver;
using FreeSql.DataAnnotations;
using Model;

namespace DAL
{
    public class Product
    {
        //获取所有商品信息
        public List<ProductInfo> GetProduct()
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                List<ProductInfo> Info;
                Info = fsql.Select<ProductInfo>()
                    .ToList();
                return Info;
            }
            catch
            {
                return null;
            }
        }
        //根据id获取单个商品信息
        public ProductInfo GetProductById(string Pid)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                List<ProductInfo> Info;
                Info = fsql.Select<ProductInfo>()
                    .Where(a => a.Pid == Pid)
                    .ToList();
                return Info[0];
            }
            catch
            {
                return null;
            }
        }
        //根据tag获取所有该tag下的商品信息
        public List<ProductInfo> GetProuctByTag(string tag)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                List<ProductInfo> Info;
                Info = fsql.Select<ProductInfo>()
                    .Where(a => a.Ptag == tag)
                    .ToList();
                return Info;
            }
            catch
            {
                return null;
            }
        }
        //根据id删除单个商品信息
        public int DeleteProduct(string Pid)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                fsql.Delete<ProductInfo>()
                   .Where(a => a.Pid == Pid)
                   .ExecuteAffrows();
                return 0;
            }
            catch
            {
                return -1;
            }
        }
        //根据商品id修改商品信息
        public int UpdateProduct(string Pid, ProductInfo proinfo)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                fsql.Update<ProductInfo>()
                    .Set(a => a.Pname, proinfo.Pname)
                    .Set(a => a.Pintro, proinfo.Pintro)
                    .Set(a => a.Pdate, proinfo.Pdate)
                    .Set(a => a.Pnum, proinfo.Pnum)
                    .Set(a => a.Psale, proinfo.Psale)
                    .Set(a => a.Pprice, proinfo.Pprice)
                    .Set(a => a.Ptag, proinfo.Ptag)
                    .Where(a => a.Pid == Pid)
                    .ExecuteAffrows();
                return 0;
            }
            catch
            {
                return -1;
            }
        }
        //根据商品id添加商品信息
        public int InsertProduct(ProductInfo proinfo)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                fsql.Insert(proinfo)
                    .ExecuteAffrows();
                return 0;
            }
            catch
            {
                return -1;
            }
        }
        //展示商品图片
        public byte[] GetProductImg(string Pid)
        {
            try
            {
                IMongoDatabase mongodb = DAL.SqlHelper.GetMongoClient();
                var collection = mongodb.GetCollection<BsonDocument>("ProImg");
                var query = new QueryDocument { { "ID", Pid } };
                var result = collection.Find(query);
                byte[] img = (byte[])(result.ToList()[0]).GetValue("Img");
                return img;
            }
            catch
            {
                return null;
            }
        }
        //向MongoDB数据库中添加图片
        public int InsertProductImg(string Pid, byte[] newimg)
        {
            try
            {
                IMongoDatabase database = DAL.SqlHelper.GetMongoClient();
                BsonDocument doc = new BsonDocument();
                doc["ID"] = Pid;
                doc["Img"] = newimg;
                var collection = database.GetCollection<BsonDocument>("ProImg");
                collection.InsertOneAsync(doc);
                return 0;
            }
            catch
            {
                return -1;
            }
        }
        //向MongoDB数据库中更改图片
        public int UpdateProductImg(string Pid, byte[] newimg)
        {
            try
            {
                IMongoDatabase database = DAL.SqlHelper.GetMongoClient();
                var col = database.GetCollection<BsonDocument>("ProImg");
                var query = new QueryDocument { { "ID", Pid } }; //定义获取“ID”值为“Pid”的查询条件
                //定义更新文档
                var update = new UpdateDocument { { "$set", new QueryDocument { { "Img", newimg } } } };
                //执行更新操作
                col.UpdateOneAsync(query, update);
                return 0;
            }
            catch
            {
                return -1;
            }
        }
        //向MongoDB数据库中删除图片
        public int DeleteProductImg(string Pid)
        {
            try
            {
                IMongoDatabase database = DAL.SqlHelper.GetMongoClient();
                //获取集合
                var col = database.GetCollection<BsonDocument>("ProImg");
                //定义获取“ID”值为Pid的查询条件
                var query = new QueryDocument { { "ID", Pid } };
                //执行删除操作
                col.DeleteMany(query);
                return 0;
            }
            catch
            {
                return -1;
            }
        }
        //根据rcmd属性是否为true返回推荐商品列表
        public List<ProductInfo> GetRcmdProducts()
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                List<ProductInfo> Info;
                Info = fsql.Select<ProductInfo>()
                    .Where(a => a.rcmd == true)
                    .ToList();
                return Info;
            }
            catch
            {
                return null;
            }
        }
        //根据传入的日期推荐最新上架的商品
        public List<ProductInfo> GetNewProducts(DateTime today)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                List<ProductInfo> Info;
                Info = fsql.Select<ProductInfo>()
                    .Where(a => (a.Pdate <= today.AddDays(10) && a.Pdate >= today.AddDays(-10)))
                    .ToList();
                return Info;
            }
            catch
            {
                return null;
            }
        }
        //模糊查询搜索的商品名
        public List<ProductInfo> GetProductsByname(string name)
        {
            try
            {
                IFreeSql fsql = DAL.SqlHelper.GetFreeSql();
                List<ProductInfo> Info;
                Info = fsql.Select<ProductInfo>()
                           .WithSql("select * from ProductInfo where Pname like '%" + name + "%'")
                           .Page(1, 10)
                           .ToList();
                return Info;
            }
            catch
            {
                return null;
            }
        }
    }
}
